package com.surpassun.emall.service.impl;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.surpassun.emall.model.Comment;
import com.surpassun.emall.model.Product;
import com.surpassun.emall.model.Store;
import com.surpassun.emall.repository.CommentRepository;
import com.surpassun.emall.repository.ProductRepository;
import com.surpassun.emall.repository.StoreRepository;
import com.surpassun.emall.service.CommentService;
import com.surpassun.emall.util.Validator;

/**
 *
 * @author Denis
 */
@Service
public class CommentServiceImpl extends AbstractServiceImpl<Comment> implements CommentService {

	@Inject
	CommentRepository commentRepository;
	
	@Inject
	ProductRepository productRepository;
	
	@Inject
	StoreRepository storeRepository;
	
    public CommentServiceImpl() {
    	super(Comment.class);
    }

	@Override
	public void addCommentForProduct(long userId, long productId, String commentContent, int rating) {
		if (Validator.isNotNull(commentContent)) {
			Product product = productRepository.get(productId);
			if (Validator.isNotNull(product)) {
				Store store = product.getStore();
				Comment comment = new Comment(userId, store, product, rating, commentContent);
				commentRepository.add(comment);
			}
		}
	}

	@Override
	public void addCommentForStore(long commentId, long userId, long storeId, String commentContent, int rating) {
		if (Validator.isNotNull(commentContent)) {
			Store store = storeRepository.get(storeId);
			if (Validator.isNotNull(store)) {
				Comment comment = new Comment(userId, store, rating, commentContent);
				commentRepository.add(comment);
			}
		}
	}

	@Override
	public void updateComment(long commentId, String commentContent, int rating) {
		Comment comment = commentRepository.get(commentId);
		if (Validator.isNotNull(comment) && Validator.isNotNull(commentContent)) {
			comment.setComment(commentContent);
			comment.setRating(rating);
			commentRepository.update(comment);
		}
	}
}